package com.mz.ozy.utils;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;

import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2018/12/20.
 * Android 彩色字体
 */

public class ColorText {

    private CharSequence mCharSequence;

    ColorText(CharSequence charSequence) {
        mCharSequence = charSequence;
    }

    public CharSequence getCharSequence() {
        return this.mCharSequence;
    }

    public static class Build {
        private LinkedHashMap<String, Integer> data = new LinkedHashMap<>();
        private SpannableStringBuilder ssb = new SpannableStringBuilder();

        public Build add(String s, int color) {
            this.data.put(s, color);
            return this;
        }

        public ColorText create() {

            if (data != null) {
                int start = 0;
                int end = 0;
                for (String key : data.keySet()) {
                    ssb.append(key);
                    end = ssb.length();
                    ssb.setSpan(new ForegroundColorSpan(data.get(key)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    start = end;
                }
            }

            return new ColorText(ssb);
        }

    }
}
